<?php
/**
 * Created by PhpStorm.
 * User: liuhongchuan
 * Date: 2018/4/25
 * Time: 10:09
 */
namespace app\operation\controller;

use app\common\controller\OperationBase;
use think\Cache;
use think\Db;

class Statistic extends OperationBase {
    public function _initialize() {
        parent::_initialize();
    }

    /**
     * 数据统计页面
     */
    public function index() {
        $data = Cache::get("operation_statistic");
        if (empty($data)) {
            $data = $this->getData();
            Cache::set("operation_statistic", $data, 600);
        }

        return $this->ajax_success($data);
    }

    /**
     * 获取门店个数
     */
    public function getShops()
    {
       $shops = DB::table("m_shop")->alias('s')
           ->join("m_api_template at", "s.api_version = at.api_version", "left")
           ->where(array("s.parent_id" => array("gt", 0)))
           ->field('s.id,at.name,count(1) as count')
           ->group("s.api_version")
           ->select();
       if (empty($shops)) {
           $this->error("获取失败");
       }
       $this->assign("shops", $shops);
       echo $this->fetch();
    }

    /**
     * 刷新缓存
     */
    public function refresh()
    {
        $data = $this->getData();
        Cache::set("operation_statistic", $data, 600);
        $this->ajax_success("刷新成功");
    }

    /**
     * 获取数据
     */
    private function getData()
    {
        $data = array();

        //今日刷卡次数
        $data["card_toady_times"] = db('behavior_log')->where(array("behavior_status" => array("eq", 1), "behavior_type" => array("eq", 1)))->whereTime('create_time', 'd')->count(1);

        //当日刷卡人数
        $data["card_today_users"] = db('behavior_log')->where(array("behavior_status" => array("eq", 1), "behavior_type" => array("eq", 1)))->whereTime('create_time', 'd')->count("distinct(card_no)");

        //员工卡数
        $data['staff_cards'] = db("interface")->where(array("owner_type" => array("eq", 2), "is_original" => array("eq", 1)))->count(1);

        //会员卡数
        $data["member_cards"] = db("interface")->where(array("owner_type" => array("eq", 1), "is_original" => array("eq", 1)))->count(1);

        //临时卡数
        $data["temporary_cards"] = db("interface")->where(array("owner_type" => array("eq", 3), "is_original" => array("eq", 1)))->count(1);

        //品牌个数
        $data["brands"] = db("shop")->where(array("parent_id" => array("eq", 0)))->count(1);

        //门店个数
        $data["shops"] = db("shop")->where(array("parent_id" => array("gt", 0)))->count(1);

        //成功刷卡总计
        $data["card_success_times"] = db('behavior_log')->where(array("behavior_status" => array("eq", 1), "behavior_type" => array("eq", 1)))->count(1);

        //失败刷卡总次
        $data["card_fail_times"] = db('behavior_log_error')->where(array("behavior_status" => array("eq", 2), "behavior_type" => array("eq", 1)))->count(1);

        //更新时间
        $data["update_time"] = date("Y-m-d H:i:s");

        return $data;
    }

    /**
 * 门店统计详情
 */
    public function allShops()
    {
        echo $this->fetch();
    }

    public function ajaxAllShops()
    {
        $shop_name = input("shop_name/s");
        $draw = input("draw",0) + 1;//请求时间
        $order_column = input("orderColumn/s", 'shop_id');
        $order_dir = input("orderDir/s", 'asc');
        $order_str = 'se.' . $order_column . ' ' . $order_dir;
        $where = array();
        if (!empty($shop_name)) {
            $where["s.shop_name"] = array("like", "%{$shop_name}%");
        }
        $total = db("shop_extra")->alias('se')
            ->join('m_shop s', 's.id = se.shop_id', 'left')
            ->where($where)
            ->count(1);
        $list = db("shop_extra")->alias('se')
            ->join('m_shop s', 's.id = se.shop_id', 'left')
            ->field('se.*, s.shop_name')
            ->where($where)
            ->order($order_str)
            ->limit($this->get_limit())
            ->select();
        $data = array('pageData' => $list, 'total' => $total, "draw" => $draw);
        $this->ajax($data);
    }

    /**
     * 门店统计详情
     */
    public function shopDay()
    {
        $this->assign("now",date("Y-m-d", time() - 86400));
        echo $this->fetch();
    }

    public function ajaxShopDay()
    {
        $shop_name = input("shop_name/s");
        $draw = input("draw",0) + 1;//请求时间
        $order_column = input("orderColumn/s", 'shop_id');
        $order_dir = input("orderDir/s", 'asc');
        $start = input("start/s", date("Y-m-d"));
        $end = input("end/s", date("Y-m-d"));
        if (empty($start)) {
            $start = date('Y-m-d');
        }
        if (empty($end)) {
            $end = date('Y-m-d');
        }

        $where = array();
        $start = str_replace('-', '', $start);
        $end = str_replace('-', '', $end);
        if ($end < $start) {
            $this->error("起始时间不能大于结束时间");
        }
        $where["ss.date"] = array("between", array($start, $end));
        if (!empty($shop_name)) {
            $where["s.shop_name"] = array("like", "%{$shop_name}%");
        }
        $order_str = 'ss.' . $order_column . ' ' . $order_dir;
        $total = db("shop_statistic")->alias('ss')
            ->join('m_shop s', 's.id = ss.shop_id', 'left')
            ->where($where)
            ->count(1);
        $list = db("shop_statistic")->alias('ss')
            ->join('m_shop s', 's.id = ss.shop_id', 'left')
            ->field('ss.*, s.shop_name')
            ->where($where)
            ->order($order_str)
            ->limit($this->get_limit())
            ->select();
        $data = array('pageData' => $list, 'total' => $total, "draw" => $draw);
        $this->ajax($data);
    }
}